package com.pengbei.enshrine.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.pengbei.enshrine.R;
import com.pengbei.enshrine.common.utils.CommonUtils;


public class AgreementDialog extends Dialog {
    private RelativeLayout rl_rdp;
    private TextView tv_rdp_title;
    private TextView tv_rdp_content;
    private TextView tv_rdp_cancal;
    private TextView tv_rdp_sure;
    private Display display;
    private int YSZCSTART = 0;
    private int YSZCEND = 0;
    private int FWTKSTART = 0;
    private int FWTKEND = 0;

    private Context context;

    private AgreementCallBack agreementCallBack;

    public AgreementDialog(@NonNull Context context) {
        super(context);
        this.context = context;
        init();
    }

    public AgreementDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.AlertDialogStyle);
        this.context = context;
        init();
    }

    protected AgreementDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
        init();
    }

    private void init(){
        setContentView(R.layout.dialog_agreement);
        CommonUtils.starAnimation(getWindow().getDecorView());//增加弹性动画
        initStatusBar();

        // 设置对话框的透明背景
        ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
        colorDrawable.setAlpha(8); // 设置透明度（0-255）
        getWindow().setBackgroundDrawable(colorDrawable);

        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度为全屏
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT; // 高度自适应内容
        getWindow().setAttributes(layoutParams);

        setCanceledOnTouchOutside(false);

        rl_rdp = (RelativeLayout) findViewById(R.id.rl_rdp);
        tv_rdp_title = (TextView) findViewById(R.id.tv_rdp_title);
        tv_rdp_content = (TextView) findViewById(R.id.tv_rdp_content);
        tv_rdp_cancal = (TextView) findViewById(R.id.tv_rdp_cancal);
        tv_rdp_sure = (TextView) findViewById(R.id.tv_rdp_sure);

        YSZCSTART = 6;
        YSZCEND = 10;
        FWTKSTART = 22;
        FWTKEND = 26;

        SpannableStringBuilder builder = new SpannableStringBuilder(tv_rdp_content.getText().toString());

        builder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {

            }

            //去除连接下划线
            @Override
            public void updateDrawState(TextPaint ds) {
                /**set textColor**/
                ds.setColor(context.getResources().getColor(R.color.color_447CD8));
                /**Remove the underline**/
                ds.setUnderlineText(false);
            }

        }, YSZCSTART, YSZCEND, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


        builder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {

            }

            //去除连接下划线
            @Override
            public void updateDrawState(TextPaint ds) {
                /**set textColor**/
                ds.setColor(context.getResources().getColor(R.color.color_447CD8));
                /**Remove the underline**/
                ds.setUnderlineText(false);
            }

        }, FWTKSTART, FWTKEND, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tv_rdp_content.setText(builder);
        tv_rdp_content.setMovementMethod(LinkMovementMethod.getInstance());
        tv_rdp_content.setHighlightColor(context.getResources().getColor(android.R.color.transparent));

        tv_rdp_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                agreementCallBack.Agree();
            }
        });

        tv_rdp_cancal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                agreementCallBack.Cancal();
            }
        });

    }

    public void setAgreementCallBack(AgreementCallBack agreementCallBack) {
        this.agreementCallBack = agreementCallBack;
    }

    public interface AgreementCallBack{
        void Agree();
        void Cancal();
    }

    private void initStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    @Override
    public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            // 拦截虚拟退出按钮的点击事件
            dismiss();
            agreementCallBack.Cancal();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

}